﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Jamhee.Noctiluca.Common;
using System.Web;

namespace Jamhee.Noctiluca.LightWiki
{
    class UrlQueryResult
    {
        private PageType _type;
        private ActionType _action;

        #region Constants

        private const string QUERY_ACTION = "action";
        private const string QUERY_ACTION_ADD = "add";
        private const string QUERY_ACTION_DELETE = "delete";
        private const string QUERY_ACTION_DOWNLOAD = "download";
        private const string QUERY_ACTION_EDIT = "edit";
        //private const string QUERY_ACTION_VIEW = "view";
        private const string QUERY_ACTION_SAVE = "save";

        private const string QUERY_PAGETYPE = "type";
        //private const string QUERY_PAGETYPE_WIKI = "wiki";
        private const string QUERY_PAGETYPE_ATTACHMENT = "attachment";

        #endregion

        public UrlQueryResult(PageType pageType, ActionType actionType)
        {
            _type = pageType;
            _action = actionType;
        }

        public static UrlQueryResult Parse(Uri url)
        {
            PageType type = PageType.Wiki;
            ActionType action = ActionType.View;

            NameValueCollection queries = HttpUtility.ParseQueryString(url.Query);

            // Get the "action" value from the query string
            string queryAction = queries.Get(QUERY_ACTION);
            if (queryAction != null)
            {
                if (queryAction.Equals(QUERY_ACTION_ADD))
                {
                    action = ActionType.Add;
                }
                else if (queryAction.Equals(QUERY_ACTION_DELETE))
                {
                    action = ActionType.Delete;
                }
                else if (queryAction.Equals(QUERY_ACTION_DOWNLOAD))
                {
                    action = ActionType.Download;
                }
                else if (queryAction.Equals(QUERY_ACTION_EDIT))
                {
                    action = ActionType.Edit;
                }
                else if (queryAction.Equals(QUERY_ACTION_SAVE))
                {
                    action = ActionType.Save;
                }
            }

            // Get the "type" value from the query string
            string queryPageType = queries.Get(QUERY_PAGETYPE);
            if (queryPageType != null)
            {
                if (queryPageType.Equals(QUERY_PAGETYPE_ATTACHMENT))
                {
                    type = PageType.Attachment;
                }
            }

            return new UrlQueryResult(type, action);
        }

        public static string BuildQueryUrl(string url, UrlQueryResult result)
        {
            return url + "?type=" + result._type + "&action=" + result._action;
        }

        public PageType Type
        {
            get
            {
                return _type;
            }
        }

        public ActionType Action
        {
            get
            {
                return _action;
            }
        }

        public enum PageType
        {
            Wiki,
            Attachment
        }

        public enum ActionType
        {
            View,
            Add,
            Edit,
            Delete,
            Download,
            Save
        }
    }
}
